﻿
Type.registerNamespace('Simple.Controls');
function pageLoad() {
    $create(Simple.Controls.ImageButton, { 'hoverImageUrl': 'Images/updateh.gif' }, { 'click': buttonClicked }, null, $get('btnClient'));
}

/////////////////////////////////////////////

Simple.Controls.ImageButton = function(element) 
{
    //Module level variables
    this._hoverImageUrl = '';
    this._text = '';
    this._originalImageUrl = '';

    //Required Event Handlers
    this._mouseOverHandler = null;
    this._mouseOutHandler = null;
    this._clickHandler = null;

    //Calling the base class constructor
    Simple.Controls.ImageButton.initializeBase(this, [element]);
}

Simple.Controls.ImageButton.prototype =
{

    //Getter for hoverImage Property
    get_hoverImageUrl : function()
    {
        return this._hoverImageUrl;
    },

    //Getter for text Property
    get_text: function() {
        return this._text;
    },

    //Setter for hoverImage Property
    set_hoverImageUrl : function(value)
    {
        var e = Function._validateParams(arguments, [{name: 'value', type: String}]);
        if (e) throw e;

        if (this._hoverImageUrl != value)
        {
            // Only sets the value if it differs from the current
            this._hoverImageUrl = value;
            //Raise the propertyChanged event
            this.raisePropertyChanged('hoverImageUrl'); //This is a base class method which resides in Sys.Component
        }
    },

    //Setter for text Property
    set_text: function(value) {
        var e = Function._validateParams(arguments, [{ name: 'value', type: String}]);
        if (e) throw e;

        if (this._text != value) {
            // Only sets the value if it differs from the current
            this._text = value;
            //Raise the propertyChanged event
            this.raisePropertyChanged('text'); //This is a base class method which resides in Sys.Component
        }
    },

    initialize : function()
    {
        //Call the base class method
        Simple.Controls.ImageButton.callBaseMethod(this, 'initialize');

        var target = this.get_element();
        this._originalImageUrl = target.src; //Store the actual image url

        this._mouseOverHandler = Function.createDelegate(this, this._onMouseOver);
        this._mouseOutHandler = Function.createDelegate(this, this._onMouseOut);
        this._clickHandler = Function.createDelegate(this, this._onClick)

        //Attach the required event handlers
        $addHandlers(target, {'mouseover':this._mouseOverHandler, 'mouseout':this._mouseOutHandler, 'click': this._clickHandler}, this);
    },

    dispose : function()
    {
        $clearHandlers(this.get_element()); // Detach all event handlers

        delete this._mouseOverHandler;
        delete this._mouseOutHandler;
        delete this._clickHandler;

        //Call the base class method
        Simple.Controls.ImageButton.callBaseMethod(this, 'dispose');
    },

    add_click : function(handler)
    {
        this.get_events().addHandler('click', handler);
    },

    remove_click : function(handler)
    {
        this.get_events().removeHandler('click', handler);
    },

    _onMouseOver : function(e)
    {
        e.target.src = this._hoverImageUrl;
    },

    _onMouseOut : function(e)
    {
        e.target.src = this._originalImageUrl;
    },

    _onClick : function(e)
    {
        e.preventDefault();

        var handler = this.get_events().getHandler('click');

        // Check if there is any subscriber of this event
        if (handler != null)
        {
            handler(this, Sys.EventArgs.Empty);
        }
    }
}

Simple.Controls.ImageButton.registerClass('Simple.Controls.ImageButton', Sys.UI.Control);

if (typeof(Sys) != 'undefined')
{
    Sys.Application.notifyScriptLoaded();
}

function buttonClicked(sender, e) {
    alert('I am clicked from Client');
    return false;
}

function buttonClickedServer(sender, e) {
    alert('I am clicked from Server');
    return false;
}
